package com.py.credit.controller;

import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Column;
import com.py.credit.entity.CompanyGenealogy;
import com.py.credit.entity.GenealogyNode;
import com.py.credit.service.ColumnService;
import com.py.credit.service.CompanyGenealogyService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.base.controller.AdvanceSearchController;
import com.py.framework.core.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业族谱查询控制器
 *
 * @author YaoS
 * @date 2019-04-15 10:04
 */
@RestController
@RequestMapping(value = "/companyGenealogy")
public class CompanyGenealogyController extends AdvanceSearchController<CompanyGenealogyService> {
    @Autowired
    CompanyGenealogyService companyGenealogyService;

    @Autowired
    ColumnService columnService;

    /**
     * 功能描述: 获取企业族谱详情
     *
     * @return 企业族谱信息
     * @author YaoS
     * @date 19/4/15 10:06
     */
    @IgnoreAuthority
    @RequestMapping(value = "/getDetail.json")
    public RestServiceResult<CompanyGenealogy> getDetail(String id) {
        CompanyGenealogy detail = companyGenealogyService.getDetail(id);
        return new RestServiceResult<>(detail);
    }


    /**
     * 功能描述: 获取企业族谱信息
     *
     * @param A000001 企业名称
     * @return 企业族谱接口信息
     * @author YaoS
     * @date 19/4/15 10:06
     */
    @IgnoreAuthority
    @RequestMapping(value = "/getGenealogy.json")
    public RestServiceResult<List> getGenealogy(String A000001) {
        List<GenealogyNode> list = companyGenealogyService.getGenealogy(A000001);
        return new RestServiceResult<>(list);
    }
   /**
    * 
    * @Title: getGenealogy
    * @Description: TODO(获取企业族谱每个成员的详细信息)
    * @param name	名称
    * @param type	类型
    * @param @return    设定文件
    * @return RestServiceResult<List>    返回类型
    * @throws
    */
    @IgnoreAuthority
    @RequestMapping(value = "/getGenealogyDetail.json")
    public RestServiceResult<Map> getGenealogyDetail(String name,String type) {
    	Map<String,Object> list = companyGenealogyService.getGenealogyDetail(name,type);
        return new RestServiceResult<>(list);
    }
    @RequestMapping(value = "/addGenealogy.json")
    public RestServiceResult<String> addGenealogy(CompanyGenealogy companyGenealogy) {
        companyGenealogyService.addCompanyGenealogy(companyGenealogy);
        return new RestServiceResult<>();
    }

    @RequestMapping(value = "/delGenealogy.json")
    public RestServiceResult<String> delGenealogy(
            @RequestParam(value = "ids", defaultValue = "") List<String> ids) {
        companyGenealogyService.delCompanyGenealogy(ids);
        return new RestServiceResult<>();
    }

    @RequestMapping(value = "/updateGenealogy.json")
    public RestServiceResult<String> updateGenealogy(CompanyGenealogy companyGenealogy) {
        companyGenealogyService.updateCompanyGenealogy(companyGenealogy);
        return new RestServiceResult<>();
    }

    /**
     * 功能描述: 获取所有的表
     *
     * @return
     * @author YaoS
     * @date 19/4/16 18:05
     */
    @RequestMapping(value = "/getTableList.json")
    public RestServiceResult<List> getTableList() {
        List<Map<String, Object>> list = companyGenealogyService.getTableList();
        return new RestServiceResult<>(list);
    }

    /**
     * 功能描述: 获取所有的表字段
     *
     * @param id 表id
     * @return columns 所有的字段
     * @author YaoS
     * @date 19/4/16 18:19
     */
    @RequestMapping(value = "/getColumnList.json")
    public RestServiceResult<List> getColumnList(Integer id) {
        List<Column> columns = columnService.getListByTableId(id);
        return new RestServiceResult<>(columns);
    }

    /**
     * 功能描述: 简单分页查询
     *
     * @param
     * @return 查询结果
     * @author YaoS
     * @date 19/4/18 12:49
     */
    @RequestMapping(value = "/getListForPage.json")
    public RestServiceResult getListForPage(Page page,
                                            @RequestParam(value = "associateName", defaultValue = "") String associateName,
                                            @RequestParam(value = "resourceTitle", defaultValue = "") String resourceTitle) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("associateName", associateName);
        params.put("resourceTitle", resourceTitle);
        List<CompanyGenealogy> list = companyGenealogyService.getListForPage(params, page);
        for(CompanyGenealogy companyGenealogy : list){
        	List<Column> columns = columnService.getListByTableId(Integer.valueOf(companyGenealogy.getTableId()));
        	for(Column c:columns){
        		if(c.getMetricCode().equals(companyGenealogy.getAssociateColumn())){
        			companyGenealogy.setAssociateColumn(c.getMetricName());
        		}
        		if(c.getMetricCode().equals(companyGenealogy.getShowColumn())){
        			companyGenealogy.setShowColumn(c.getMetricName());
        		}
        	}
        }
        
        return new RestServiceResult<>(page.getTotalCount(), list);
    }
}


